package com.hnkywl.sts.service.cwgl;

import com.hnkywl.sts.dao.jsgl.FpDao;
import com.hnkywl.sts.dao.jsgl.JsdhbDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.cggl.Jsdhb;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.jsgl.FpService;
import com.hnkywl.sts.service.jsgl.XsjsdService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:58
 * To change this template use File | Settings | File Templates.
 */
@Service
public class FphbService {
    private JsdhbDao jsdhbDao;
    private FpDao fpDao;
    private FpdrService fpdrService;

    @Transactional
    public String findAllfp(String hbrName,String voucherNum,Long gsId, String gsJc,Date hbrqStart,Date hbrqEnd,int pageNo, int pageSize) {
        Page<Jsdhb> fphbPage = this.jsdhbDao.findfphbList(hbrName,voucherNum,gsId, gsJc,hbrqStart,hbrqEnd,pageNo, pageSize);
        List<Jsdhb> fphbList = fphbPage.getResult() == null ? new ArrayList<Jsdhb>() : fphbPage.getResult();
        String fphbJson = BaseUtils.fetchCollectionToJson(fphbList, "id","gs.id","gs.qc","hbrq","hbdh","kjqj","voucherNum","hbr.id","hbr.xm");
        return "{\"total\":" + fphbPage.getTotalCount() + ",\"rows\":[" + fphbJson + "]}";
    }

    @Transactional
    public void fphbCancel(Long jsdhbId) {
      Jsdhb jsdhb = jsdhbDao.get(jsdhbId);
      jsdhb.setDeleted(1);
      jsdhb.setLastUpdated(new Date());
      jsdhbDao.save(jsdhb);
       List<Jsd> jsdList = fpDao.findAllJsdByJsdhbId(jsdhbId);
           for(Jsd j : jsdList){
               j.setJsdhb(null);
               fpDao.save(j);
           }
    }


    public Map findFphbVoucherMain(Long fphbId){
        Jsdhb jsdhb = jsdhbDao.get(fphbId);
        return fpdrService.findVoucherMain(jsdhb.getJsdList().get(0).getId());
    }

    public Jsdhb findFphbDetail(Long fphbId){
        Jsdhb jsdhb = jsdhbDao.get(fphbId);
        return jsdhb;
    }

    public List<Map> getFphbVoucherItem(Long fphbId){
        Jsdhb jsdhb = jsdhbDao.get(fphbId);
        List<Map> voucherItem = new ArrayList<Map>();
        for (Jsd jsd : jsdhb.getJsdList()){
            voucherItem.addAll(fpdrService.getVoucherItem(jsd.getId()));
        }
        return voucherItem;
    }


    @Transactional
    public Map getVoucher(Long id, String uuid,Integer year,Integer mouth,Map voucherMaster) {
        Jsdhb jsdhb = jsdhbDao.get(id);
        List<Jsd> jsdList = jsdhb.getJsdList();
        Map<String, Object> result = new HashMap<String, Object>();
        Map master = findFphbVoucherMain(jsdList.get(0).getId());
        master.put("id", uuid);
        master.put("acctyear", year != null ? year : "");//会计年度
        master.put("acctperiod", mouth != null ? mouth : "");//会计期间
        master.put("acctbookcode", "01");//账薄标识
        master.put("attachment",voucherMaster.get("attachment"));//附件张数
        master.put("createdate",voucherMaster.get("createdate"));//附件张数
        master.put("memo","");//备注
        result.put("master", master);
        List<Map> items = getFphbVoucherItem(id);
        for(Map m : items){
            m.put("id", m.get("itemorder"));//唯一标识
            m.put("vchrid", uuid);//凭证主表ID
            m.put("price", "0");//单价
            m.put("currencycode", "CNY");//货币代码
            m.put("exchrate", "1.000000");//汇率
        }
        result.put("master",items);
        return result;
    }

    public Map getRevokeInfo(Long id) {
        Map<String, Object> revokeInfo = new HashMap<String, Object>();
        Jsdhb result = jsdhbDao.get(id);
        if (result == null) {
            throw new RuntimeException("未获取凭证信息!");
        }
        revokeInfo.put("id", result.getUuid());//凭证ID
        revokeInfo.put("vchrid", result.getVoucherId());//凭证ID
        revokeInfo.put("unitcode", result.getGs().getKmdm());//单位代码
        revokeInfo.put("acctyear", result.getAcctyear());//会计年度
        return revokeInfo;
    }

    @Transactional
    public Jsdhb findfphbDetail(Long id) {
        return this.jsdhbDao.get(id);
    }
    @Autowired
    public void setJsdhbDao(JsdhbDao jsdhbDao) {
        this.jsdhbDao = jsdhbDao;
    }
    @Autowired
    public void setFpDao(FpDao fpDao) {
        this.fpDao = fpDao;
    }
    @Autowired
    public void setFpdrService(FpdrService fpdrService) {
        this.fpdrService = fpdrService;
    }
}
